<?php

namespace application\Module;

use application\Model\HouseProductRelatModel;
use library\Core\Db\CDbCriteria;
use application\Model\HouseModel;
use application\Comm\Request;
use application\Comm\Func;
use library\Core\Config;

class House
{

    public function attribute()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'description' => '描述',
            'type_id' => '户型',
            'designer_id' => '设计师',
            'status' => '状态',
            'orderby' => '排序',
            'src' => '图片',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    public function getList($select = [], $type_id = 0,$style_id = 0,$acreage_id = 0, $limit = 4)
    {
        $criteria = new CDbCriteria();
        $criteria->select($select);

        $page['now_page'] = Request::getParam('p') ? Request::getParam('p') : 1;
        $page['list_rows'] = Request::getParam('list_rows') ? Request::getParam('list_rows') : $limit;
        $offset = ($page['now_page'] - 1) * $page['list_rows'];

        //排序
        $orderby = (int)Request::getParam('orderby');
        $ordertype = Request::getParam('ordertype');
        $orderFields = [1 => 'orderby', 2 => 'price', 3 => 'update_time', 3 => 'view',];
        $orderField = isset($orderFields[$orderby]) ? $orderFields[$orderby] : 'orderby';
        if ($type_id) $criteria->addEqCondition('type_id', $type_id);
        if ($style_id) $criteria->addEqCondition('style_id', $style_id);
        if ($acreage_id) $criteria->addEqCondition('acreage', $acreage_id);
       
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);

        if ($ordertype == 'asc') {
            $criteria->addOrderAsc($orderField);
        } else {
            $criteria->addOrderDesc($orderField);
        }
        $houseModel = new HouseModel();
        $page['total_rows'] = $houseModel->getCount($criteria);

        $criteria->setOffset($offset);
        $criteria->setLimit($page['list_rows']);

        $data = $houseModel->getListForArray($criteria);
        return ['data' => $data, 'page' => $page];
    }
    /**
     * 根据全屋ID获取产品
     * @param array $condition
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getShow($id, $select = ['id'])
    {
    	
        $criteria = new CDbCriteria();
        $criteria->select($select);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('house_id', $id);
       
        $str = $criteria->addRightJoin(\application\Table\Product::getInstance(), 'house_product_relat.product_id=product.id', 'id,name,thumb,price,price_id,is_show_price,category_id');
        
        $houseProductRelatModel = new HouseProductRelatModel();
       
        $data = $houseProductRelatModel->getListForArray($criteria);
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['thumb'] = Func::imgUrl($v['thumb']);
            }
        }
        return $data;
    }
    /**
     * 根据全屋ID获取产品
     * @param array $condition
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getInfo($id, $select = ['id','name','src','unit_src','description','designer_id'])
    {
        $criteria = new CDbCriteria();
        $criteria->select($select);
        $criteria->addEqCondition('is_del', 0);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('id', $id);
        $houseModel = new HouseModel();
        $data = $houseModel->getRow($criteria);
     
        if (!empty($data)) {
        	$data['src'] = isset($data['src']) ? Func::imgUrl($data['src']) : '';
        	$data['unit_src'] = isset($data['unit_src']) ? Func::imgUrl($data['unit_src']) : '';
        }
       
        return $data;
    }
    
    /**
     * 首页获取
     * @param array $condition
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public function getIndex($select = ['id','name','type_id','src','unit_src','description','designer_id','bdaddr','acreage','price_all'])
    {
    	$type = Config::getParamsVal('HOUSE_TYPE');
    	$acreage = Config::getParamsVal('HOUSE_ACREAGE');
    	
    	$criteria = new CDbCriteria();
    	$criteria->select($select);
    	$criteria->addEqCondition('is_del', 0);
    	$criteria->addEqCondition('status', 1);
    	$criteria->setLimit(1);
    	$criteria->addOrderDesc('orderby');
    	$houseModel = new HouseModel();
    	$data = $houseModel->getListForArray($criteria);
    	$designer = new Designer(); 
    	$houseproductrelatmodule = new HouseProductRelat();
    	$datas = [];
    	 foreach ($data as $key=>$val){
    	 	$datas[$key]['id'] = $val['id'];
    	 	$datas[$key]['name'] = $val['name'];
    	 	$datas[$key]['type_id'] = $type[$val['type_id']];
    	 	$datas[$key]['src'] = $houseproductrelatmodule ->Show($val['id']);
    	 	$datas[$key]['unit_src'] = isset($val['unit_src']) ? Func::imgUrl($val['unit_src']) : '';
    	 	$datas[$key]['description'] = $val['description'];
    	 	$datas[$key]['designer_id'] = isset($val['designer_id']) ? $designer->getInfo($val['designer_id'])['name'] :'';
    	 	$datas[$key]['designer_src'] = isset($val['designer_id']) ? $designer->getInfo($val['designer_id'])['src'] :'';
    	 	$datas[$key]['bdaddr'] = $val['bdaddr'];
    	 	$datas[$key]['acreage'] = $acreage[$val['acreage']];
    	 	$datas[$key]['price_all'] = $val['price_all'];
    	 }
    	
    	
    	return $datas;
    }

}
